﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BabyToys.Models;
using BabyToys.Models.ViewModel;
using PagedList;
using PagedList.Mvc;
using System.Data;
namespace BabyToys.Controllers
{
    public class AdminKhachHangController : Controller
    {
        //
        // GET: /AdminKhachHang/
        DatabaseContext db = new DatabaseContext();
        public ActionResult Index(int? page)
        {
            var khachhangs = db.KhachHangs.ToList();
            var pageNumber = page ?? 1;
            ViewBag.onePageOfKhachHang = khachhangs.ToPagedList(pageNumber, 10);
            return View();
        }
        public ActionResult SuaKhachHang(int id)
        {
            var user = db.KhachHangs.SingleOrDefault(u => u.IdKhachHang == id);
            KhachHangViewModel khachhang=new KhachHangViewModel
            //userModel = user;
            {
                Id = user.IdKhachHang,
                TenDangNhap = user.TenDangNhap,
                TenDayDu = user.TenDayDu,
                MatKhau = user.MatKhau,
                XacNhanMatKhau = user.MatKhau,
                Email = user.Email,
                DiaChi = user.DiaChi,
                SoDienThoai = user.SoDienThoai,
                IsActive = user.IsActive,
                //IsOnline = user.IsOnline
            };
            //userModel = user as RegisterModel;
            ViewBag.IdLoaiKhachHang = new SelectList(db.LoaiKhachHangs.ToList(), "IdLoaiKhachHang", "TenLoai", khachhang.IdLoaiKhachHang);
            return View(khachhang);
        }

        [HttpPost]
        public ActionResult SuaKhachHang(KhachHangViewModel register)
        {
            if (ModelState.IsValid)
            {
                BabyToys.DAL.RegisterDAL reg = new DAL.RegisterDAL();
                reg.SuaKhachHang(register);
                return RedirectToAction("Index");
            }
            ViewBag.IdLoaiKhachHang = new SelectList(db.LoaiKhachHangs.ToList(), "IdLoaiKhachHang", "TenLoai", register.IdLoaiKhachHang);
            return View(register);
        }
        [HttpPost]
        public ActionResult CapNhatTrangThai(int id, bool trangthai)
        {
            var khachhang = db.KhachHangs.SingleOrDefault(u => u.IdKhachHang == id);
            if (khachhang != null)
            {
                khachhang.IsActive = trangthai;
            }
            db.Entry(khachhang).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult LoaiKhachHang()
        {
            var loaikhachhangs = db.LoaiKhachHangs.ToList();
            return View(loaikhachhangs);
        }

        public ActionResult ThemLoaiKhachHang()
        {
            return View();
        }
        [HttpPost]
        public ActionResult ThemLoaiKhachHang(LoaiKhachHang loaikhachhang)
        {
            if (ModelState.IsValid)
            {
                db.LoaiKhachHangs.Add(loaikhachhang);
                db.SaveChanges();
                return RedirectToAction("LoaiKhachHang");
            }
            return View(loaikhachhang);
        }

        public ActionResult SuaLoaiKhachHang(int id)
        {
            var loaikhachhang = db.LoaiKhachHangs.SingleOrDefault(l => l.IdLoaiKhachHang == id);
            return View(loaikhachhang);
        }
        [HttpPost]
        public ActionResult SuaLoaiKhachHang(LoaiKhachHang loaikhachhang)
        {
            if (ModelState.IsValid)
            {
                db.Entry(loaikhachhang).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("LoaiKhachHang");
            }
            return View(loaikhachhang);
        }
    }
}
